<!-- 还车成功后的发票页面printbill.php -->
<!DOCTYPE html>
<html>
<?php
session_start();
require 'connection.php';
$conn = Connect();
?>

<head>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/customerlogin.css">
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/clientpage.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/bookingconfirm.css" />
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
    <!-- 导航栏 -->
    <?php
    include './nav.php';
    ?>

    <body>

        <?php
        // 从 GET 请求中获取 id 参数
        $id = $_GET["id"];
        $distance = NULL;

        // 从 POST 请求中获取 distance_or_days 和 hid_fare 参数，并进行数据清洗
        $distance_or_days = $conn->real_escape_string($_POST['distance_or_days']);
        $fare = $conn->real_escape_string($_POST['hid_fare']);

        // 计算总金额
        $total_amount = $distance_or_days * $fare;

        // 获取当前日期
        $car_return_date = date('Y-m-d');

        // 设置车辆归还状态
        $return_status = "R";

        // 获取当前登录的客户信息
        $login_customer = $_SESSION['login_customer'];

        // 查询已租赁的车辆信息
        $sql0 = "SELECT rc.id, rc.rent_end_date, rc.charge_type, rc.rent_start_date, c.car_name, c.car_nameplate FROM rentedcars rc, cars c WHERE id = '$id' AND c.car_id = rc.car_id";
        $result0 = $conn->query($sql0);

        // 获取查询结果
        if (mysqli_num_rows($result0) > 0) {
            while ($row0 = mysqli_fetch_assoc($result0)) {
                $rent_end_date = $row0["rent_end_date"];
                $rent_start_date = $row0["rent_start_date"];
                $car_name = $row0["car_name"];
                $car_nameplate = $row0["car_nameplate"];
                $charge_type = $row0["charge_type"];
            }
        }

        // 计算日期差
        function dateDiff($start, $end)
        {
            $start_ts = strtotime($start);
            $end_ts = strtotime($end);
            $diff = $end_ts - $start_ts;
            return round($diff / 86400);
        }

        // 计算额外天数
        $extra_days = dateDiff("$rent_end_date", "$car_return_date");
        $total_fine = $extra_days * 200;

        // 计算租赁时长
        $duration = dateDiff("$rent_start_date", "$rent_end_date");

        // 如果有额外天数，则计算总金额
        if ($extra_days > 0) {
            $total_amount = $total_amount + $total_fine;
        }

        // 根据计费类型进行不同更新操作
        if ($charge_type == "days") {
            $no_of_days = $distance_or_days;
            $sql1 = "UPDATE rentedcars SET car_return_date='$car_return_date', no_of_days='$no_of_days', total_amount='$total_amount', return_status='$return_status' WHERE id = '$id' ";
        } else {
            $distance = $distance_or_days;
            $sql1 = "UPDATE rentedcars SET car_return_date='$car_return_date', distance='$distance', no_of_days='$duration', total_amount='$total_amount', return_status='$return_status' WHERE id = '$id' ";
        }

        // 执行更新操作
        $result1 = $conn->query($sql1);

        // 如果更新成功，则更新相关表的数据
        if ($result1) {
            $sql2 = "UPDATE cars c, driver d, rentedcars rc SET c.car_availability='yes', d.driver_availability='yes' WHERE rc.car_id=c.car_id AND rc.driver_id=d.driver_id AND rc.customer_username = '$login_customer' AND rc.id = '$id'";
            $result2 = $conn->query($sql2);
        } else {
            echo $conn->error;
        }
        ?>

        <div class="container">
            <div class="jumbotron">
                <h1 class="text-center" style="color: green;"><span class="glyphicon glyphicon-ok-circle"></span> 车辆已归还</h1>
            </div>
        </div>
        <br>

        <h2 class="text-center"> 感谢您选择租车服务！祝您一路平安。 </h2>

        <h3 class="text-center"> <strong>订单编号：</strong> <span style="color: blue;"><?php echo "$id"; ?></span> </h3>


        <div class="container">
            <h5 class="text-center">请阅读关于您订单的以下信息。</h5>
            <div class="box">
                <div class="col-md-10" style="float: none; margin: 0 auto; text-align: center;">
                    <h3 style="color: orange;">我们已收到您的预订并将其放入我们的订单处理系统。</h3>
                    <br>
                    <h4>请记下您的<strong>订单号</strong>，以备将来与我们沟通有关您的订单。</h4>
                    <br>
                    <h3 style="color: orange;">发票</h3>
                    <br>
                </div>
                <div class="col-md-10" style="float: none; margin: 0 auto; ">
                    <h4> <strong>车辆名称: </strong> <?php echo $car_name; ?></h4>
                    <br>
                    <h4> <strong>车辆编号:</strong> <?php echo $car_nameplate; ?></h4>
                    <br>
                    <h4> <strong>租金:&nbsp;</strong> ￥<?php
                                                        if ($charge_type == "days") {
                                                            echo ($fare . "/天");
                                                        } else {
                                                            echo ($fare . "/公里");
                                                        }
                                                        ?></h4>
                    <br>
                    <h4> <strong>预订日期: </strong> <?php echo date("Y-m-d"); ?> </h4>
                    <br>
                    <h4> <strong>开始日期: </strong> <?php echo $rent_start_date; ?></h4>
                    <br>
                    <h4> <strong>租车结束日期: </strong> <?php echo $rent_end_date; ?></h4>
                    <br>
                    <h4> <strong>车辆归还日期: </strong> <?php echo $car_return_date; ?> </h4>
                    <br>
                    <?php if ($charge_type == "days") { ?>
                        <h4> <strong>天数:</strong> <?php echo $distance_or_days; ?>天</h4>
                    <?php } else { ?>
                        <h4> <strong>行驶里程:</strong> <?php echo $distance_or_days; ?>公里</h4>
                    <?php } ?>
                    <br>
                    <?php
                    if ($extra_days > 0) {

                    ?>
                        <h4> <strong>总罚款:</strong> <label class="text-danger"> ￥<?php echo $total_fine; ?>/- </label>，用于 <?php echo $extra_days; ?> 天额外。</h4>
                        <br>
                    <?php } ?>
                    <h4> <strong>总金额: </strong> ￥<?php echo $total_amount; ?>/- </h4>
                    <br>
                </div>
            </div>
            <div class="col-md-12" style="float: none; margin: 0 auto; text-align: center;">
                <h6>警告！<strong>请勿重新加载此页面</strong>，否则以上显示将会丢失。如果您需要此页面的纸质副本，请立即打印。</h6>
            </div>
        </div>

    </body>
    <footer class="site-footer">
        <div class="container">
            <hr>
            <div class="row">
                <div class="col-sm-6">
                    <h5>© <?php echo date("Y"); ?> 租车服务</h5>
                </div>
            </div>
        </div>
    </footer>

</html>